package com.xuyoupeng.springframework.beans.factory.support;

import com.xuyoupeng.springframework.beans.BeansException;
import com.xuyoupeng.springframework.beans.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;


import java.lang.reflect.Constructor;

/**
 * @author xuyoupneg
 * @description 在BeanFactories中使用的默认对象实例化策略
 * @date 2024/3/16 15:09
 */
public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationStrategy {

    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor constructor, Object[] args) throws BeansException {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(beanDefinition.getBeanClass());
        enhancer.setCallback(new NoOp() {
            @Override
            public int hashCode() {
                return super.hashCode();
            }
        });
        if (null == constructor) return enhancer.create();
        return enhancer.create(constructor.getParameterTypes(), args);
    }
}
